package rickyGame.game.action
{
	import flash.geom.Point;
	import flash.geom.Vector3D;
	
	import ricky.game.Actor;
	import ricky.quadtree.Leaf;
	import ricky.quadtree.Morton;
	import ricky.quadtree.Quadtree;
	import ricky.utils.FrameManager;
	
	import ricky.game.Actor2D;
	import rickyGame.game.Fighter;
	import rickyGame.game.GameState;
	import rickyGame.game.Player;
	import rickyGame.game.scene.Pathfinder;
	import rickyGame.game.skill.SkillData;
	
	/**
	 * 寻找攻击范围行动
	 */
	public class ArriveAttackRangeAct extends MoveLeafAction
	{
		public function ArriveAttackRangeAct(actor:Actor2D,target:Actor,point:Point)
		{
			super(actor, null, null);
			_target=target;
			_player=Fighter(actor);
			if(target)
			{
				targetX=Actor2D(target).gridX;
				targetY=Actor2D(target).gridY;
			}else
			{
				targetX=point.x/Common.tileWidth;
				targetY=point.y/Common.tileHeight;
			}
		}
		
		override public function begin():void
		{
			if(_player)
			{
				skill=_player.skills.attack1;
				var tree:Quadtree=GameState.inst.scene.tree;
				var first:Leaf=tree.getLeaf(Actor2D(_actor).morton);
				var m:int=Morton.morton(targetY,targetX);
				var destinationNode:Leaf=tree.getLeaf(m);
				var arr:Array=Pathfinder.findPath1(first,destinationNode,tree,Pathfinder.traversableFunction,_actor.asType);
				_path=arr;
				var i:int=skill.maxCastSkillDistance;
				this._radius=i;
					
				if(!_path || _path.length==0)
				{
					this._next=null;
					this._effectNext=null;
					this.end();
					return;
				}
				moveStep();
				if(this._point)
					super.begin();
			}
		}
		
		override protected function moveStep():void
		{
			if(_path!=null && _path.length>=1)
			{
				var tree:Quadtree=GameState.inst.scene.tree;
				var me:Leaf=tree.getLeaf(Actor2D(_actor).morton);
				var leaf:Leaf;
				
				if(_target)
				{
					if(targetX!=Actor2D(_target).gridX || targetY!=Actor2D(_target).gridY) //如果目标已经移动了					
					{
						targetX=Actor2D(_target).gridX;
						targetY=Actor2D(_target).gridY;
							
						leaf=tree.getLeaf(Actor2D(_target).morton);
						_path=Pathfinder.findPath1(me,leaf,tree,Pathfinder.traversableAroundFunction,_actor.asType);
					}
				}
				
				leaf=_path.shift();//目标位置
				if(!leaf)
				{
					this._point=new Vector3D(this._actor.graphic.x,this._actor.graphic.y);
					return;
				}
				
				if(canWalk(leaf))//如果目标可走
				{
					this._point=new Vector3D(leaf.x*Common.tileWidth+20,leaf.y*Common.tileHeight+15);
				}else
				{
					var iswalk:Boolean=false;//=this.canWalk(leaf);
					
					while(!iswalk)//可走
					{
						leaf=_path.shift();
						if(leaf)
							iswalk=this.canWalk(leaf);
						else
							iswalk=true;
					}
					
					if(!leaf)
					{
						this._point=new Vector3D(this._actor.graphic.x,this._actor.graphic.y);
						return;
					}
					
					var arr:Array=Pathfinder.findPath1(me,leaf,tree,Pathfinder.traversableAroundFunction,_actor.asType);
					
					if(arr)
					{
						_path=arr.concat(_path);
					
						leaf=_path.shift();
						this._point=new Vector3D(leaf.x*Common.tileWidth+20,leaf.y*Common.tileHeight+15);
					}
				}
				
			}else
				this._point=new Vector3D(this._actor.graphic.x,this._actor.graphic.y);
		}
		
		//
		private var _target:Actor;//
		private var _player:Fighter;
		private var skill:SkillData;

		private var targetX:int;
		private var targetY:int;
	}
}